home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / invest / preferences.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  5.4 KB  |  156 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from gettext import gettext as _
  5. from os.path import join
  6. import gtk
  7. import gobject
  8. import gconf
  9. import invest
  10. from gettext import gettext as _
  11. import cPickle
  12.  
  13. class PrefsDialog:
  14.     
  15.     def __init__(self, applet):
  16.         self.ui = gtk.Builder()
  17.         self.ui.add_from_file(join(invest.BUILDER_DATA_DIR, 'prefs-dialog.ui'))
  18.         self.dialog = self.ui.get_object('preferences')
  19.         self.treeview = self.ui.get_object('stocks')
  20.         self.ui.get_object('add').connect('clicked', self.on_add_stock)
  21.         self.ui.get_object('add').connect('activate', self.on_add_stock)
  22.         self.ui.get_object('remove').connect('clicked', self.on_remove_stock)
  23.         self.ui.get_object('remove').connect('activate', self.on_remove_stock)
  24.         self.treeview.connect('key-press-event', self.on_tree_keypress)
  25.         self.typs = (str, float, float, float)
  26.         self.names = (_('Symbol'), _('Amount'), _('Price'), _('Commission'))
  27.         store = gtk.ListStore(*self.typs)
  28.         self.treeview.set_model(store)
  29.         self.model = store
  30.         
  31.         def on_cell_edited(cell, path, new_text, col, typ):
  32.             
  33.             try:
  34.                 store[path][col] = typ(new_text)
  35.             except:
  36.                 pass
  37.  
  38.  
  39.         
  40.         def get_cell_data(column, cell, model, iter, data):
  41.             (typ, col) = data
  42.             if typ == int:
  43.                 cell.set_property('text', '%d' % typ(model[iter][col]))
  44.             elif typ == float:
  45.                 cell.set_property('text', '%.2f' % typ(model[iter][col]))
  46.             else:
  47.                 cell.set_property('text', typ(model[iter][col]))
  48.  
  49.         
  50.         def create_cell(view, column, name, typ):
  51.             cell_description = gtk.CellRendererText()
  52.             cell_description.set_property('editable', True)
  53.             cell_description.connect('edited', on_cell_edited, column, typ)
  54.             column_description = gtk.TreeViewColumn(name, cell_description)
  55.             if typ == str:
  56.                 column_description.set_attributes(cell_description, text = 0)
  57.             
  58.             if typ == float:
  59.                 column_description.set_cell_data_func(cell_description, get_cell_data, (float, column))
  60.             
  61.             view.append_column(column_description)
  62.  
  63.         for n in xrange(0, 4):
  64.             create_cell(self.treeview, n, self.names[n], self.typs[n])
  65.         
  66.         stock_items = invest.STOCKS.items()
  67.         stock_items.sort()
  68.         for key, purchases in stock_items:
  69.             for purchase in purchases:
  70.                 store.append([
  71.                     key,
  72.                     purchase['amount'],
  73.                     purchase['bought'],
  74.                     purchase['comission']])
  75.             
  76.         
  77.         
  78.         try:
  79.             pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(join(invest.ART_DATA_DIR, 'invest-16.png'), -1, -1)
  80.             self.dialog.set_icon(pixbuf)
  81.         except Exception:
  82.             (None, (None,))
  83.             msg = (None, (None,))
  84.         except:
  85.             (None, (None,))
  86.  
  87.         self.sync_ui()
  88.  
  89.     
  90.     def show_run_hide(self, explanation = ''):
  91.         expl = self.ui.get_object('explanation')
  92.         expl.set_markup(explanation)
  93.         self.dialog.show_all()
  94.         if explanation == '':
  95.             expl.hide()
  96.         
  97.         self.dialog.run()
  98.         self.dialog.destroy()
  99.         invest.STOCKS = { }
  100.         
  101.         def save_symbol(model, path, iter):
  102.             if model[iter][0] not in invest.STOCKS:
  103.                 invest.STOCKS[model[iter][0]] = []
  104.             
  105.             invest.STOCKS[model[iter][0]].append({
  106.                 'amount': float(model[iter][1]),
  107.                 'bought': float(model[iter][2]),
  108.                 'comission': float(model[iter][3]) })
  109.  
  110.         self.model.foreach(save_symbol)
  111.         
  112.         try:
  113.             cPickle.dump(invest.STOCKS, file(invest.STOCKS_FILE, 'w'))
  114.             if invest.DEBUGGING:
  115.                 print 'Stocks written to file'
  116.         except Exception:
  117.             msg = None
  118.             if invest.DEBUGGING:
  119.                 print 'Could not save stocks file:', msg
  120.             
  121.         except:
  122.             invest.DEBUGGING
  123.  
  124.  
  125.     
  126.     def sync_ui(self):
  127.         pass
  128.  
  129.     
  130.     def on_add_stock(self, w):
  131.         self.treeview.get_model().append([
  132.             'GOOG',
  133.             0,
  134.             0,
  135.             0])
  136.  
  137.     
  138.     def on_remove_stock(self, w):
  139.         (model, paths) = self.treeview.get_selection().get_selected_rows()
  140.         for path in paths:
  141.             model.remove(model.get_iter(path))
  142.         
  143.  
  144.     
  145.     def on_tree_keypress(self, w, event):
  146.         if event.keyval == 65535:
  147.             self.on_remove_stock(self, w)
  148.         
  149.         return False
  150.  
  151.  
  152.  
  153. def show_preferences(applet, explanation = ''):
  154.     PrefsDialog(applet).show_run_hide(explanation)
  155.  
  156.